//
//  StrategySecondViewController.m
//  ITrip
//
//  Created by dllo on 15/5/26.
//  Copyright (c) 2015年 lanou3g.com 蓝鸥科技. All rights reserved.
//

#import "StrategySecondViewController.h"
#import "TravelNotesViewController.h"
#import "SubjectViewController.h"


@interface StrategySecondViewController ()

@end

@implementation StrategySecondViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];

    UIImage *icon = [UIImage imageNamed:@"iconfont_return"];
    icon = [icon imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:icon style:UIBarButtonItemStyleDone target:self action:@selector(getBack)];
    
    self.view.backgroundColor = [UIColor colorWithRed:227/255.0 green:233/255.0 blue:240/255.0 alpha:1];
    
    UILabel *title = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 100, 30)];
    NSString *str = @"攻略";
    title.text = [self.titleName stringByAppendingString:str];
    title.font = [UIFont systemFontOfSize:20];
    title.font = [UIFont boldSystemFontOfSize:25];
    title.textColor = [UIColor whiteColor];
    self.navigationItem.titleView = title;
    
    // 定义菊花控件
    UIActivityIndicatorView *activityIndicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
    [self.view addSubview:activityIndicator];
    activityIndicator.color = [UIColor grayColor];
    activityIndicator.center = self.view.center;
    [activityIndicator release];
    [activityIndicator startAnimating];
    
    
    NSString *path = [URLPath StrategySecondURL];
    self.arrayData = [NSMutableArray array];
    
    //下载
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    path = [path stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    NSString *string = [path stringByAppendingFormat:@"%ld.json?page=1", self.number];
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    NSDictionary *params = nil;
    [manager GET:string parameters:params success:^(AFHTTPRequestOperation *operation, id responseObject) {
        self.arrayData = [StrategySecondParser getParser:operation.responseData];
        [self createMyView];
        [activityIndicator stopAnimating];
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        [activityIndicator stopAnimating];
    }];
}

- (void)getBack
{
    self.tabBarController.tabBar.hidden = NO;
    self.tabBarController.tabBar.translucent = NO;
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)createMyView
{
    myTableView = [[UITableView alloc]initWithFrame:CGRectMake(10, 10, self.view.frame.size.width - 20, self.view.frame.size.height - 5) style:UITableViewStylePlain];
    [self.view addSubview:myTableView];
    myTableView.dataSource = self;
    myTableView.delegate = self;
    myTableView.showsVerticalScrollIndicator = NO;
    [myTableView release];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *identifier = @"cell";
    StrategySecondTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (cell == nil) {
        cell = [[StrategySecondTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
    }
    StrategySecondModel *model = [self.arrayData objectAtIndex:indexPath.row];
    [cell setStrategySecondModel:model];
    
    
    if (indexPath.row < self.arrayData.count) {
        [cell.strategyButton addTarget:self action:@selector(touchStrategyButton:) forControlEvents:UIControlEventTouchUpInside];
        cell.strategyButton.tag = indexPath.row;
        
        [cell.journeyButton addTarget:self action:@selector(touchJourneyButton:) forControlEvents:UIControlEventTouchUpInside];
        cell.journeyButton.tag = indexPath.row;
        
        [cell.addressButton addTarget:self action:@selector(touchAddressButton:) forControlEvents:UIControlEventTouchUpInside];
        cell.addressButton.tag = indexPath.row;
        
        [cell.topicButton addTarget:self action:@selector(touchTopicButton:) forControlEvents:UIControlEventTouchUpInside];
        cell.topicButton.tag = indexPath.row;
        
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    return cell;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.arrayData.count;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 300;
}

// 专题点击事件
- (void)touchTopicButton:(UIButton *)button
{
    self.tabBarController.tabBar.hidden = YES;
    self.tabBarController.tabBar.translucent = YES;
    SubjectViewController *subject = [[SubjectViewController alloc] init];
    StrategySecondModel *model = [self.arrayData objectAtIndex:button.tag];
    subject.topicId = model.secondID;
    subject.IsTopic = 1;
    [self.navigationController pushViewController:subject animated:YES];
    [subject release];
}

//攻略的点击事件
- (void)touchStrategyButton:(UIButton *)button
{
    NSIndexPath *path = [NSIndexPath indexPathForRow:button.tag inSection:0];
    // 取出cell
    StrategySecondTableViewCell *cell = [myTableView cellForRowAtIndexPath: path];
    if (cell.strategyButtonTag == 1) {
        StrategyForeignStrategyViewController *foreignStrategy = [[StrategyForeignStrategyViewController alloc]init];
        StrategySecondModel *model = [self.arrayData objectAtIndex:button.tag];
        foreignStrategy.titleName = model.second_name_zh_cn;
        foreignStrategy.numberStrategy = model.secondID;
        [self.navigationController pushViewController:foreignStrategy animated:YES];
        [foreignStrategy release];
    } else {
        TravelNotesViewController *trabelNotes = [[TravelNotesViewController alloc] init];
        self.tabBarController.tabBar.hidden = YES;
        self.tabBarController.tabBar.translucent = YES;
        StrategySecondModel *model = [self.arrayData objectAtIndex:button.tag];
        trabelNotes.numberStrategy = model.secondID;
        [self.navigationController pushViewController:trabelNotes animated:YES];
        [trabelNotes release];
    }
    
}

//行程的点击事件
- (void)touchJourneyButton:(UIButton *)button
{
    StrategyForeignJourneyViewController *foreignJourney = [[StrategyForeignJourneyViewController alloc]init];
    StrategySecondModel *model = [self.arrayData objectAtIndex:button.tag];
    foreignJourney.titleName = model.second_name_zh_cn;
    foreignJourney.numberJourney = model.secondID;
    [self.navigationController pushViewController:foreignJourney animated:YES];
    [foreignJourney release];
}

//旅行地的点击事件
- (void)touchAddressButton:(UIButton *)button
{
    self.tabBarController.tabBar.hidden = YES;
    self.tabBarController.tabBar.translucent = YES;
    StrategyForeignAddressViewController *foreignAddress = [[StrategyForeignAddressViewController alloc]init];
    StrategySecondModel *model = [self.arrayData objectAtIndex:button.tag];
    foreignAddress.titleName = model.second_name_zh_cn;
    foreignAddress.numberAddress = model.secondID;
    [self.navigationController pushViewController:foreignAddress animated:YES];
    [foreignAddress release];
}




- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
